package com.yc.book.dao;

import com.yc.common.util.DBHelper;

import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookDao {

    public Map<String, Object> queryPage() {
        String sql = "select * from books where 1=1";
        List<Map<String, Object>> list = DBHelper.selectListMap(sql);
        long count = DBHelper.count(sql);
        Map<String, Object> page = new HashMap<>();
        page.put("rows", list);
        page.put("total", count);

        return page;
    }


    //page 第几页
    //size 每页行数

    public Map<String, Object> queryPage(int page, int size) {
        page--;
        String sql = "select * from books limit ?,?";
        String sql1 = "select *from books";
        List<Map<String, Object>> list = DBHelper.selectListMap(sql, page * size, size);
        long count = DBHelper.count(sql1);
        Map<String, Object> data = new HashMap<>();
        data.put("rows", list);
        data.put("total", count);

        return data;
    }

    public Map<String, Object> queryPage(String name, String date1, String date2, int ipage, int isize) {
        String sql = "select * from books where 1=1";
        List<Object> params = new ArrayList();
        if (name != null && name.trim().isEmpty() == false) {
            name = "%" + name + "%";
            sql += " and (bookname like ? or bookauthor like ? or bookpress like ? ) ";
            params.add(name);
            params.add(name);
            params.add(name);
        }
        if (date1 != null && date1.trim().isEmpty() == false) {
            sql += " and pressdate >= ?";
            params.add(date1);
        }
        if (date2 != null && date2.trim().isEmpty() == false) {
            sql += " and pressdate <= ?";
            params.add(date2);
        }

        ipage--;
        String sql1 = sql + " limit ?,?";
        String sql2 = sql;
        long count = DBHelper.count(sql2, params.toArray());
        params.add(ipage * isize);
        params.add(isize);
        List<Map<String, Object>> list = DBHelper.selectListMap(sql1, params.toArray());
        Map<String, Object> data = new HashMap<>();

        data.put("rows", list);
        data.put("total", count);

        System.out.println(data);
        return data;
    }

    public void delete(String id) {
        String sql = "delete from books where id=?";
        DBHelper.update(sql, id);

    }

    public long countByName(String bookname, String id) {
        return DBHelper.count("select * from books where bookname = ? ", bookname, id);
    }

    public void insert(String bookname, String bookpress, String pressdate, String bookauthor, String bookcount, String bookimage) {
        String sql = "insert into books(bookname,bookpress,pressdate," +
                "bookauthor,bookcount,bookimage) values(?,?,?,?,?,?)";
        DBHelper.update(sql, bookname, bookpress, pressdate, bookauthor, bookcount, bookimage);


    }

    public void update(String id, String bookname, String bookpress, String pressdate, String bookauthor, String bookcount, String bookimage) {

        String sql = "update  set  books bookname=?, bookpress=? , pressdate=?," +
                "bookauthor=?,bookcount=?,bookimage=? where id= ? ";

        DBHelper.update(sql, bookname, bookpress, pressdate, bookauthor, bookcount, bookimage);

    }
}
